int LeftBtn = 6;
int RightBtn = 7;
int LeftState;
int LastLeftBtnState = LOW;
int RightState;
int LastRightBtnState = LOW;
int Buzzer = A0;
long lastDebounceTime = 0;
long debounceDelay = 50;
int Password = 0;
int Count = 0;

void touchBtn() {
  tone(Buzzer,300,200);
  delay(200);
  noTone(Buzzer);
}

void error() {
  int tones[]={500,500,500,500};
  for(int x = 0;x < 6; x++){
    tone(Buzzer,tones[x],400);
    delay(30);
    noTone(Buzzer);
  }
  delay(800);
  noTone(Buzzer);
}

void success(){
  int tones[]={200,300,400,500,600,700,800};
  for(int x = 0;x < 6; x++){
    tone(Buzzer,tones[x],300);
  }
  delay(1000);
  noTone(Buzzer);
}

void setup() {
  // put your setup code here, to run once:
  pinMode(LeftBtn, INPUT);
  pinMode(RightBtn, INPUT);
  pinMode(Buzzer, OUTPUT);
  Serial.begin(9600);
  Password = 0;
}

void loop() {

  int leftBtnState = digitalRead(LeftBtn);
  int rightBtnState = digitalRead(RightBtn);

  if (leftBtnState != LastLeftBtnState) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (leftBtnState != LeftState) {
      LeftState = leftBtnState;
      if (leftBtnState == HIGH) {
        Password ++;
        Count++;
        touchBtn();
      }
    }
  }
  LastLeftBtnState = leftBtnState;

  if (rightBtnState != LastRightBtnState) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (rightBtnState != RightState) {
      RightState = rightBtnState;
      if (rightBtnState == HIGH) {
        Password --;
        Count++;
        touchBtn();
      }
    }
  }
  LastRightBtnState = rightBtnState;

  if (Password == 2 && Count == 6) {
    Serial.println("密码正确");
    Password = 0;
    Count = 0;
    success();
  }

  if (Count > 6) {
    Serial.println("输入错误");
    Password = 0;
    Count = 0;
    error();
    delay(500);
  }
}



